package com.todo.biz.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.biz.infrastructure.entity.po.BizCounterPO;
import com.todo.biz.infrastructure.mapper.BizCounterMapper;
import com.todo.biz.infrastructure.repository.BizCounterRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BizCounter)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("bizCounterRepository")
public class BizCounterRepositoryImpl implements BizCounterRepository {
    @Resource
    private BizCounterMapper bizCounterMapper;


    @Override
    public Boolean removeBizCounterPOById(Long id) {
        if (id == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        BizCounterPO bizCounterPO = queryBizCounterPOById(id);
        if (bizCounterPO == null) {
            return true;
        }
        return bizCounterMapper.removeBizCounterPOById(id) > 0;
    }

    @Override
    public List<BizCounterPO> queryBizCounterListByType(Long businessId, Integer businessScene) {
        if (businessId == null || businessScene == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return bizCounterMapper.queryBizCounterListByType(businessId, businessScene);
    }

    @Override
    public BizCounterPO queryBizCounterPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return bizCounterMapper.queryBizCounterPOById(id);
    }

    @Override
    public Boolean insertBizCounterPO(BizCounterPO bizCounterPO) {
        if (bizCounterPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!baseFieldCheck(bizCounterPO)) {
            return false;
        }

        return bizCounterMapper.insertBizCounterPO(bizCounterPO) > 0;
    }


    @Override
    public Boolean updateBizCounterPOById(BizCounterPO bizCounterPO) {
        if (bizCounterPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!baseFieldCheck(bizCounterPO)) {
            return false;
        }
        BizCounterPO alreadyBizCounterPO = queryBizCounterPOById(bizCounterPO.getId());
        if (alreadyBizCounterPO == null) {
            log.warn("BizCounterRepository#updateBizCounterPOById 数据不存在，更新失败 ：【{}】", JSONObject.toJSONString(bizCounterPO));
            return false;
        }
        log.info("BizCounterRepository#updateBizCounterPOById  数据更新开始，更新业务id：【{}】，更新业务类型：【{}】，更新前数量：【{}】，更新后数量：【{}】", alreadyBizCounterPO.getBusinessId(), alreadyBizCounterPO.getBusinessScene(), alreadyBizCounterPO.getCount(), bizCounterPO.getCount());
        return bizCounterMapper.updateBizCounterPOById(bizCounterPO) > 0;
    }

    @Override
    public BizCounterPO queryBizCounterByBizIdAndSceneAndType(Long bizId, Integer scene, Integer type) {
        if (bizId == null || scene == null || type == null){
            return null;
        }
        return bizCounterMapper.queryBizCounterByBizIdAndSceneAndType(bizId, scene, type);
    }

    private boolean baseFieldCheck(BizCounterPO bizCounterPO) {
        if (bizCounterPO == null) {
            return false;
        }
        if (bizCounterPO.getBusinessId() == null || bizCounterPO.getCount() == null) {
            log.error("BizCounterRepository#baseFieldCheck 基础字段校验异常，必填数据为空 ：【{}】", JSONObject.toJSONString(bizCounterPO));
            return false;
        }
        if (bizCounterPO.getCounterType() == null || bizCounterPO.getBusinessScene() == null) {
            log.error("BizCounterRepository#baseFieldCheck 基础字段校验异常，类型数据为空 ：【{}】", JSONObject.toJSONString(bizCounterPO));
            return false;
        }
        return true;
    }
}
